package com.net.xpay.admin.filter;

import com.net.shard.ShardUtil;

import javax.servlet.*;
import java.io.IOException;

/**
 * 2019/4/27 15:35
 */
public class ShardContextFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            //由于事务必须要有一个分片信息，就默认设置第一个分片 防止报错
            ShardUtil.setShardContext(0L);
            chain.doFilter(request, response);
        } finally {
            ShardUtil.cleanShardContext();
        }
    }

    @Override
    public void destroy() {

    }
}
